package com.zcf.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zcf.common.json.Body;
import com.zcf.mapper.WithdrawMapper;
import com.zcf.pojo.PumpingLog;
import com.zcf.pojo.User;
import com.zcf.pojo.Withdraw;
import com.zcf.vo.out.LayuiJson;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

/**
 * @author jj
 * @date 2019/11/22 15:17
 */
@Service
public class WithdrawServiceImpl {
    @Autowired
    WithdrawMapper withdrawMapper;
    /**
    * @description :添加提现
    * @param withdraw 
    * @date : 2019/11/29 10:54
    * @author : jj
    */
    @Transactional(rollbackFor = Exception.class)
    public Body add(Withdraw withdraw)  {
        if (StringUtils.isEmpty(withdraw.getAccount())){
            return Body.newInstance(301,"账户不能为空！");
        }
        if (withdraw.getMoney()==null||withdraw.getMoney()==0){
            return Body.newInstance(302,"提现金币不能为空！");
        }
        if (withdraw.getUserid()==null||withdraw.getUserid()==0){
            return Body.newInstance(303,"未知错误！");
        }
        if (withdraw.getType()==null||withdraw.getType()==0){
            return Body.newInstance(303,"未知错误！");
        }
        User user=new User();
        user.setId(withdraw.getUserid());
        User u = user.selectById();
        if (u==null){
            return Body.newInstance(303,"未知错误！");
        }
        if (u.getMoney()<withdraw.getMoney()){
            return Body.newInstance(304,"不能超过本身拥有金额！");
        }
        u.setMoney(u.getMoney()-withdraw.getMoney());
        boolean b = u.updateById();
        if (!b){
            return Body.newInstance(303,"未知错误！");
        }
        withdraw.setCreateTime(new Date());
        withdraw.setState(0);
        boolean insert = withdraw.insert();
        return insert ? Body.newInstance(200,"提现成功") : Body.newInstance(303,"未知错误！");
    }
    /**
    * @description :未审批列表
    * @param pageNum
    * @param pageSize
    * @date : 2019/11/30 9:11
    * @author : jj
    */
    public LayuiJson fandList(int pageNum, int pageSize , int type){
        LayuiJson lj=new LayuiJson();
        List<Withdraw> list = withdrawMapper.selectPage(new Page<>(pageNum, pageSize), new EntityWrapper<Withdraw>().eq("state", 0).eq("type",type));
        Integer count = withdrawMapper.selectCount(new EntityWrapper<Withdraw>().eq("state", 0).eq("type",type));
        if (list.size()>0){
                lj.setCode(0);
                lj.setCount(count);
                lj.setData(list);
                lj.setMsg("成功");
                return lj;
        }
        lj.setCode(1);
        lj.setCount(null);
        lj.setData(null);
        lj.setMsg("暂无数据");
        return lj;
    }
    /**
     * @description :历史记录
     * @param pageNum
     * @param pageSize
     * @date : 2019/11/30 9:11
     * @author : jj
     */
    public LayuiJson fandAllList(int pageNum, int pageSize){
        Object[] ids = new Object[]{1,2};
        LayuiJson lj=new LayuiJson();
        List<Withdraw> list = withdrawMapper.selectPage(new Page<>(pageNum, pageSize), new EntityWrapper<Withdraw>().in("state", ids));

        Integer count = withdrawMapper.selectCount(new EntityWrapper<Withdraw>().in("state",ids));
        if (list.size()>0){
            lj.setCode(0);
            lj.setCount(count);
            lj.setData(list);
            lj.setMsg("成功");
            return lj;
        }
        lj.setCode(1);
        lj.setCount(null);
        lj.setData(null);
        lj.setMsg("暂无数据");
        return lj;
    }
    /**
    * @description :更新审批状态
    * @date : 2019/11/30 9:12
    * @author : jj
    */
    @Transactional(rollbackFor = Exception.class)
    public Body update(Withdraw withdraw){
        if (withdraw.getId()!=null){
            Withdraw withdraw1 = withdraw.selectById();
            if (withdraw1!=null){
                if (withdraw.getState()==2){
                    User user=new User();
                    user.setId(withdraw1.getUserid());
                    User user1 = user.selectById();
                    if (user1 != null){
                        user1.setMoney(withdraw1.getMoney());
                        boolean b = user1.updateById();
                        if (b){
                            withdraw1.setState(2);
                            boolean b1 = withdraw1.updateById();
                            return b1 ? Body.newInstance() : Body.BODY_470;
                        }

                    }
                }
                if (withdraw.getState()==1){
                    withdraw1.setState(1);
                    boolean b1 = withdraw1.updateById();
                    return b1 ? Body.newInstance() : Body.BODY_470;
                }
            }
        }
        return Body.BODY_470;
    }

}
